package com.venue.hjt.venueclass.fragment

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.StaggeredGridLayoutManager
import android.util.Log
import android.view.View
import com.venue.hjt.venueclass.R
import com.venue.hjt.venueclass.adapter.PhotoAdapter
import com.venue.hjt.venueclass.db.SchAlbum
import com.venue.hjt.venueclass.model.PhotoFModel
import com.venue.hjt.venueclass.utlis.Constant

/**
 * Created by xqx on 2018/3/13.
 *
 */
class TabPhotoFragment : BaseFragment(){
    var fPhotoReceiver:FPhotoReceiver?=null
    override fun initView(view: View?) {
        recyclerView = view!!.findViewById(R.id.recycler_view)
    }
    var adapter: PhotoAdapter?=null
    val list=ArrayList<SchAlbum>()
    var model: PhotoFModel?=null
    override fun loadData() {
        fPhotoReceiver=FPhotoReceiver()
        activity.registerReceiver(fPhotoReceiver,filter())
        //使用瀑布流布局,第一个参数 spanCount 列数,第二个参数 orentation 排列方向
        model= PhotoFModel()
        val recyclerViewLayoutManager =
                StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
//        val pool1 = recyclerView!!.recycledViewPool
//        pool1.setMaxRecycledViews(0, 0)
//        recyclerView!!.recycledViewPool = pool1
        recyclerView!!.layoutManager = recyclerViewLayoutManager
//        val list=ArrayList<String>()
//        list.add("http://smart.gcihjt.com/file/down/upload_photo_2017_12_20171227093238211.jpg")
//        list.add("http://smart.gcihjt.com/file/down/upload_photo_2017_12_20171227093238278.jpg")
//        list.add("http://smart.gcihjt.com/file/down/upload_photo_2017_12_20171227092207920.jpg")
//        list.add("http://smart.gcihjt.com/file/down/upload_photo_2017_12_20171227092207568.jpg")
//        list.add("http://smart.gcihjt.com/file/down/upload_photo_2017_12_20171227092207652.jpg")
//        list.add("http://smart.gcihjt.com/file/down/upload_photo_2017_12_20171227092207980.jpg")
//        list.add("http://smart.gcihjt.com/file/down/upload_photo_2017_12_20171227092208563.jpg")
//        list.add("http://smart.gcihjt.com/file/down/upload_photo_2017_12_20171227093238211.jpg")
//        list.add("http://smart.gcihjt.com/file/down/upload_photo_2017_12_20171227093238278.jpg")
//        list.add("http://smart.gcihjt.com/file/down/upload_photo_2017_12_20171227092207920.jpg")
        list+=model!!.SchBanner("1")
        adapter = PhotoAdapter(activity,list)
        recyclerView!!.adapter=adapter
    }

    override fun getLayoutResource(): Int {
        return  R.layout.fragment_photo
    }
    var recyclerView: RecyclerView?=null
    /**
     * 广播类
     */
    inner class FPhotoReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val action = intent.action
            when(action){
                Constant.BANNER->{
                    Log.e("ATG","执行删除3")
                    list.clear()
                    list+=model!!.SchBanner("1")
                    adapter!!.notifyDataSetChanged()
                }
            }

        }
    }
    /**
     * @return IntentFilter 广播
     */
    private fun filter(): IntentFilter {
        val filter = IntentFilter()
        filter.addAction(Constant.BANNER)
        return filter
    }
}